package edu.gatech.bsbmule.model;

import java.awt.Point;

/**
 * This class represents an individual Tile that hold information
 * of itself (where it is, what kind of tile it is, and the mule 
 * that may be assigned to it)
 * 
 * @author BSB
 * @version 1.0 10/8/2013
 */

public class Tile {
	
	private Point location;
	private int terrain;
	private Mule mule;
	
	public static final int TERRAIN_TOWN = 1;
	public static final int TERRAIN_PLAIN = 2;
	public static final int TERRAIN_RIVER = 3;
	public static final int TERRAIN_M1 = 4;
	public static final int TERRAIN_M2 = 5;
	public static final int TERRAIN_M3 = 6;
	
	/**
	 * Instantiates a tile at 'location' on the map
	 * of type 'type'
	 * @param type
	 * @param location
	 */
	public Tile(int type, Point location) {
		this.location = location;
		terrain = type;
		mule = null;
	}
	
	/**
	 * Sets the location of the Tile
	 * @param location
	 */
	public void setLocation(Point location) {
		this.location = location;
	}
	
	/**
	 * Returns the location of the Tile
	 * @return
	 */
	public Point getLocation() {
		return location;
	}
	
	/**
	 * Returns the terrain
	 * @return
	 */
	public int getTerrain() {
		return terrain;
	}
	
	/**
	 * Gets the mule from the player and adds it to the
	 * Tile. If there is already a mule on the Tile, tells player
	 * that he/she wasted the mule
	 * @param m
	 */
	public void addMule(Mule m) {
		if (!isUsed())
			mule = m;
		else
			System.out.println("There is already a mule there. You wasted your mule.");
	}
	
	/**
	 * removes the mule
	 */
	public void removeMule() {
		mule = null; 
	}
	/**
	 * Returns a boolean of whether of not the Tile
	 * has a mule
	 * @return
	 */
	public boolean isUsed() {
		return mule != null;
	}
	
	//Avoid circular reference
	/*
	public boolean isAvailable() {
		return (owner == null);
	}

	public void setOwner(Player p) {
		owner = p;
	}
	
	public void removeOwner() {
		owner = null; 
	}
	*/
	
	/**
	 * Returns a String that contains the location and terrain of the Tile
	 */
	public String toString() {
		return "Tile at: " + location.getX() + "," + location.getY()
			+ " with terrain of: " + terrain;
	}

	/**
	 * Get product produced by mule
	 * 
	 * @return a int array containing the product produced by
	 * the mule that turn. It returns an array so the Player can
	 * know what kind of resource the tile is producing :
	 *  Format is [food, energy, smithore]
	 */
	public int[] getProduct() {
		int[] out = new int[3];
		if (mule.getMuleType() == Mule.MULE_FOOD){
			if (this.getTerrain()==Tile.TERRAIN_RIVER)
				out[0] = 4;
			else if (this.getTerrain()==Tile.TERRAIN_PLAIN)
				out[0] = 2;
			else
				out[0] = 1;
		}
		else if (mule.getMuleType() == Mule.MULE_ENERGY){
			if (this.getTerrain()==Tile.TERRAIN_RIVER)
				out[1] = 2;
			else if (this.getTerrain()==Tile.TERRAIN_PLAIN)
				out[1] = 3;
			else
				out[1] = 1;
		}
		else
		{
			if (this.getTerrain()==Tile.TERRAIN_RIVER)
				out[2] = 0;
			else if (this.getTerrain()==Tile.TERRAIN_PLAIN)
				out[2] = 1;
			else if (this.getTerrain()==Tile.TERRAIN_M1)
				out[2] = 2;
			else if (this.getTerrain()==Tile.TERRAIN_M2)
				out[2] = 3;
			else if (this.getTerrain()==Tile.TERRAIN_M3)
				out[2] = 4;
		}
		return out;
			
	}
}
